// pages/my/vip/vip_grade/index.js

const app=getApp();
const payAndInput=require("../../../controllers/ControllerVip").controller.payAndInput;
const amountHistory=require("../../../controllers/ControllerVip").controller.amountHistory;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    tab1:true,
    tab2:false,
    moneyData:{},
    amountList:[],
    outList:[],
    value:{
      genre:0,
      shopId:"",
      amountType:0,
      pageNumber:1
    },
    value2:{
      genre:1,
      shopId:"",
      amountType:0,
      pageNumber:1
    },
    tabItem:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({globalStype:app.globalData.globalStype})
    if(options.shopId){
      this.setData({
        shopId:options.shopId
      })
      this.setData({
        'value.shopId':options.shopId,
        'value2.shopId':options.shopId
      })
    }else{
      this.setData({
        'value.shopId':app.globalData.shopId,
        'value2.shopId':app.globalData.shopId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPayAndInput()
    this.getAmountHistory()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getIncomeOrCost()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 进行tab跳转选项
  selectTab(e){
    var num=e.currentTarget.dataset['index']
   if(num==1){
    this.setData({tab1:true,tab2:false})
    this.setData({
      tabItem:0
    })
   }else{
    this.setData({tab2:true,tab1:false})
    this.setData({
      tabItem:1
    })
   }
  },

  //获取余额

  getPayAndInput(){
    let value={
      amountType:0,
      shopId:this.data.value.shopId
    }
    payAndInput(value).then(res=>{
      this.setData({
        moneyData:res.data
      })
    })
  },

  //获取第一页充值消费记录
  getAmountHistory(){
    let value=this.data.value
    let value2=this.data.value2
    amountHistory(value).then(res=>{
      this.setData({
        amountList:res.data.list
      })
    })
    amountHistory(value2).then(res=>{
      this.setData({
        outList:res.data.list
      })
    })
  },

  //下滑获取充值或者消费的记录
  getIncomeOrCost(){
    let selectItem=this.data.tabItem;
    if(selectItem==0){
      let page=this.data.value.pageNumber;
      page=page+1;
      this.setData({
        'value.pageNumber':page
      })
      amountHistory(this.data.value).then(res=>{
        let list1=this.data.amountList;
        let list2=res.data.list;
        let cList=list1.concat(list2);

        this.setData({
          amountList:cList
        })
      })
    }else{
      let page=this.data.value2.pageNumber;
      page=page+1;
      this.setData({
        'value2.pageNumber':page
      })
      amountHistory(this.data.value2).then(res=>{
        let list1=this.data.outList;
        let list2=res.data.list;
        let cList=list1.concat(list2);

        this.setData({
          outList:cList
        })
      })
    }
  },
  
  //跳转充值
  jumpRecharge(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_renew/index',
    })
  },

  //返回首页
  backIndex(){
    let page=getCurrentPages()
    let shopId=this.data.shopId
    console.log(page.length)
    if(page.length<=1){
      wx.redirectTo({
        url: '/pages/Index/Index?shopId='+shopId,
      })
    }
  }
})